<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Admin\Model\AuthGroupModel;
use Think\Page;

/**
 * 后台信息管理控制器
 * @author huajie <banhuajie@163.com>
 */
class InfoController extends AdminController {



    /**
     * 显示左边菜单，进行权限控制
     * @author huajie <banhuajie@163.com>
     */
    protected function getMenu(){
        //获取动态分类
        $cate_auth  =   AuthGroupModel::getAuthCategories(UID);	//获取当前用户所有的内容权限节点
        $cate_auth  =   $cate_auth == null ? array() : $cate_auth;
        $cate       =   M('Category')->where(array('status'=>1))->field('id,title,pid,allow_publish')->order('pid,sort')->select();

        //没有权限的分类则不显示
        if(!IS_ROOT){
            foreach ($cate as $key=>$value){
                if(!in_array($value['id'], $cate_auth)){
                    unset($cate[$key]);
                }
            }
        }

        $cate           =   list_to_tree($cate);	//生成分类树

        //获取分类id
        $cate_id        =   I('param.cate_id');
        $this->cate_id  =   $cate_id;

        //是否展开分类
        $hide_cate = false;
        if(ACTION_NAME != 'recycle' && ACTION_NAME != 'draftbox' && ACTION_NAME != 'mydocument'){
            $hide_cate  =   true;
        }

        //生成每个分类的url
        foreach ($cate as $key=>&$value){
            $value['url']   =   'Article/index?cate_id='.$value['id'];
            if($cate_id == $value['id'] && $hide_cate){
                $value['current'] = true;
            }else{
                $value['current'] = false;
            }
            if(!empty($value['_child'])){
                $is_child = false;
                foreach ($value['_child'] as $ka=>&$va){
                    $va['url']      =   'Article/index?cate_id='.$va['id'];
                    if(!empty($va['_child'])){
                        foreach ($va['_child'] as $k=>&$v){
                            $v['url']   =   'Article/index?cate_id='.$v['id'];
                            $v['pid']   =   $va['id'];
                            $is_child = $v['id'] == $cate_id ? true : false;
                        }
                    }
                    //展开子分类的父分类
                    if($va['id'] == $cate_id || $is_child){
                        $is_child = false;
                        if($hide_cate){
                            $value['current']   =   true;
                            $va['current']      =   true;
                        }else{
                            $value['current'] 	= 	false;
                            $va['current']      =   false;
                        }
                    }else{
                        $va['current']      =   false;
                    }
                }
            }
        }

        $this->assign('nodes',      $cate);
        $this->assign('cate_id',    $this->cate_id);

        //获取面包屑信息
        $nav = get_parent_category($cate_id);
        $this->assign('rightNav',   $nav);

        //获取回收站权限
        $show_recycle = $this->checkRule('Admin/article/recycle');
        $this->assign('show_recycle', IS_ROOT || $show_recycle);
        //获取草稿箱权限
        $this->assign('show_draftbox', C('OPEN_DRAFTBOX'));
    }


    /**
     * 信息列表
     */
    public function index(){
        $list   = $this->lists(M('info'), array('status'=>1),'id asc');
        $cateList = M("cate")->select();
        $cateList=get_id_key($cateList,'id');
        $this->assign('cateList',$cateList);
        $this->assign('_list', $list);
        $this->meta_title = '信息列表';
        $this->display();
    }

    //审核列表
    public function examine(){
        $list   = $this->lists(M('info'), array('status'=>0),'id asc');
        $cateList = M("cate")->select();
        $cateList=get_id_key($cateList,'id');
        $this->assign('cateList',$cateList);
        $this->assign('_list', $list);
        $this->meta_title = '审核信息列表';
        $this->display();
    }



    /**删除信息
     * @param array $where
     */
    public function deleteInfo() {
        $ids    =   I('request.ids');
        if(empty($ids)){
            $this->error('请选择要操作的数据');
        }
        if(is_array($ids)){
            $where['id']=array('in',$ids);
        }else{
            $where['id']=$ids;
        }

        if( M('info')->where($where)->delete() !== false) {
            $this->success('删除成功','',true);
        }else{
            $this->error('删除失败！','',true);
        }

    }


     public function changeInfoStatus(){
         $id = array_unique((array)I('ids',0));
         $id = is_array($id) ? implode(',',$id) : $id;
         $status=I('get.status');
         if ( empty($id) ) {
             $this->error('请选择要操作的数据!');
         }
         $map['id'] =   array('in',$id);
         $re=M('info')->where($map)->setField('status',$status);
         if($re!==false){
            $this->success("审核成功",'',true);
         }else{
            $this->error("审核失败",'',true);
         }
        
    }



    //信息分类
    public function cateList(){
        $list   = $this->lists(M('cate'), array(),'id asc');
        $this->assign('_list', $list);
        $this->meta_title = '分类信息';
        $this->display();
    }



    //新增信息分类
    public function addCate(){
        $this->meta_title = '新增分类';
        $this->display('editcate');
    }

    public function editCate(){
        $id=I('get.id');
        $data=M('cate')->where(array('id'=>$id))->field('id,name')->find();
        $this->assign('info',$data);
        $this->meta_title = '编辑分类';
        $this->display('editcate');
    }


    public function saveCate(){
        $data=I('post.');
        if(empty($data)){
            $this->error ("请填写完整数据");
        }
        if(empty($data['id'])){ //新增数据
            $res = M('cate')->add($data);
            if(!$res){
                $this->error ("新增出错");
            }else{
                $this->success ('新增成功！', U('cateList') );
            }
        } else { //更新数据
            $status = M('cate')->save($data);
            if(false === $status){
                $this->error ("更新出错");
            }else{
                $this->success ('更新成功！', U('cateList') );
            }
        }
    }


    /**删除分类
     * @param array $where
     */
    public function deleteCate() {
        $ids    =   I('request.ids');
        if(empty($ids)){
            $this->error('请选择要操作的数据');
        }
        if(is_array($ids)){
            $cate['id']=array('in',$ids);
        }else{
            $cate['id']=$ids;
        }

        $re1= M('cate')->where($cate)->delete();

        if( $re1 !== false) {
            $this->success('删除成功','',true);
        }else{
            $this->error('删除失败！','',true);
        }

    }


     //置顶管理
    public function topList(){
        $list   = $this->lists(M('feetype'), array(),'id asc');
        $this->assign('_list', $list);
        $this->meta_title = '置顶信息';
        $this->display();
    }


     //新增置顶管理
    public function addTop(){
        $this->meta_title = '新增置顶信息';
        $this->display('edittop');
    }

    public function editTop(){
        $id=I('get.id');
        $data=M('feetype')->where(array('id'=>$id))->find();
        $this->assign('info',$data);
        $this->meta_title = '编辑置顶信息';
        $this->display('edittop');
    }


    public function saveTop(){
        $data=I('post.');
        if(empty($data)){
            $this->error ("请填写完整数据");
        }
        if(empty($data['id'])){ //新增数据
            $res = M('feetype')->add($data);
            if(!$res){
                $this->error ("新增出错");
            }else{
                $this->success ('新增成功！', U('topList') );
            }
        } else { //更新数据
            $status = M('feetype')->save($data);
            if(false === $status){
                $this->error ("更新出错");
            }else{
                $this->success ('更新成功！', U('topList') );
            }
        }
    }


    /**删除置顶
     * @param array $where
     */
    public function deleteTop() {
        $ids    =   I('request.ids');
        if(empty($ids)){
            $this->error('请选择要操作的数据');
        }
        if(is_array($ids)){
            $cate['id']=array('in',$ids);
        }else{
            $cate['id']=$ids;
        }

        $re1= M('feetype')->where($cate)->delete();

        if( $re1 !== false) {
            $this->success('删除成功','',true);
        }else{
            $this->error('删除失败！','',true);
        }

    }






}
